home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8434 / 8434.xpi / chrome / content / rdfds.js < prev    next >
Text File  |  2008-11-16  |  18KB  |  156 lines

  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is rdfds
  13. *
  14. * The Initial Developer of the Original Code is Neil Deakin
  15. * Portions created by Neil Deakin are Copyright (C) 2002 Neil Deakin.
  16. * All Rights Reserved.
  17. *
  18. * Contributor(s):
  19. *   Jason Barnabe
  20. */
  21.  
  22. /* This is a library for easier access to RDF datasources and resources.
  23. * It contains four objects, GoogleRedesignedRDFDataSource, GoogleRedesignedRDFNode, GoogleRedesignedRDFLiteral. and
  24. * GoogleRedesignedRDFEnumerator.
  25. *
  26. * An RDF DataSource is a graph of nodes and literals. The constructor
  27. * for GoogleRedesignedRDFDataSource takes one argument, a URI of an RDF file to use.
  28. * If the URI exists, the contents of the RDF file are loaded. If it
  29. * does not exist, resources can be added to it and then written using
  30. * this save method. If the URL argument is null, a blank datasource
  31. * is created.
  32. *
  33. * This library is designed for convenience not for efficiency.
  34. *
  35. * The API is documented at:
  36. *   http://www.xulplanet.com/tutorials/xultu/rdfds/
  37. *
  38. * Example:
  39. *
  40. * var ds=new GoogleRedesignedRDFDataSource("file:///main/mozilla/mimtest.rdf");
  41. * var node=ds.getNode("http://www.example.com/sample");
  42. * var child=ds.getNode("http://www.example.com/child");
  43. * child=node.addChild(child);
  44. * child.addTarget("http://www.xulplanet.com/rdf/xpimaker#appname","Find Files");
  45. * ds.save();
  46. *
  47. */
  48. var RDFService = "@mozilla.org/rdf/rdf-service;1";
  49. RDFService = Components.classes[RDFService].getService();
  50. RDFService = RDFService.QueryInterface(Components.interfaces.nsIRDFService);
  51. var RDFContainerUtilsService = "@mozilla.org/rdf/container-utils;1";
  52. RDFContainerUtilsService = Components.classes[RDFContainerUtilsService].getService();
  53. RDFContainerUtilsService = RDFContainerUtilsService.QueryInterface(Components.interfaces.nsIRDFContainerUtils);
  54. function RDFLoadObserver() {
  55. }
  56. RDFLoadObserver.prototype = {callback: null, callbackDataSource: null, Init: function (c, cDS) {this.callback = c;this.callbackDataSource = cDS;}, QueryInterface: function (iid) {if (iid.equals(Components.interfaces.nsIRDFXMLSinkObserver)) {return this;} else {throw Components.results.NS_ERROR_NO_INTERFACE;}}, onBeginLoad: function (sink) {}, onInterrupt: function (sink) {}, onResume: function (sink) {}, onError: function (sink, status, msg) {}, onEndLoad: function (sink) {if (this.callback != null) {this.callback(this.callbackDataSource);}}};
  57. function GoogleRedesignedRDFDataSource(uri, callbackFn) {
  58.     if (uri == null) {
  59.         this.datasource = null;
  60.     } else {
  61.         this.load(uri, callbackFn);
  62.     }
  63. }
  64. GoogleRedesignedRDFDataSource.prototype.load = function (uri, callbackFn) {if (uri.indexOf(":") == -1) {var docurl = document.location.href;if (document.location.pathname == null) {uri = docurl + "/" + uri;} else {uri = docurl.substring(0, docurl.lastIndexOf("/") + 1) + uri;}}if (callbackFn == null) {this.datasource = RDFService.GetDataSourceBlocking(uri);} else {this.datasource = RDFService.GetDataSource(uri);var ds;try {ds = this.datasource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);} catch (ex) {callbackFn(this);return;}if (ds.loaded) {callbackFn(this);return;}var packObserver = new RDFLoadObserver;packObserver.Init(callbackFn, this);var rawsource = this.datasource;rawsource = rawsource.QueryInterface(Components.interfaces.nsIRDFXMLSink);rawsource.addXMLSinkObserver(packObserver);}};
  65. GoogleRedesignedRDFDataSource.prototype.Init = function (dsource) {this.datasource = dsource;};
  66. GoogleRedesignedRDFDataSource.prototype.parseFromString = function (str, baseUri) {if (this.datasource == null) {this.makeemptyds();}var ios = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);baseUri = ios.newURI(baseUri, null, null);var xmlParser = Components.classes['@mozilla.org/rdf/xml-parser;1'].createInstance(Components.interfaces.nsIRDFXMLParser);xmlParser.parseString(this.datasource, baseUri, str);};
  67. GoogleRedesignedRDFDataSource.prototype.serializeToString = function () {var outputStream = {data: "", close: function () {}, flush: function () {}, write: function (buffer, count) {this.data += buffer;return count;}, writeFrom: function (stream, count) {}, isNonBlocking: false};this.serializeToStream(outputStream);return outputStream.data;};
  68. GoogleRedesignedRDFDataSource.prototype.serializeToStream = function (outputStream) {var ser = Components.classes['@mozilla.org/rdf/xml-serializer;1'].createInstance(Components.interfaces.nsIRDFXMLSerializer);ser.init(this.datasource);ser.QueryInterface(Components.interfaces.nsIRDFXMLSource).Serialize(outputStream);};
  69. GoogleRedesignedRDFDataSource.prototype.makeemptyds = function (uri) {this.datasource = Components.classes['@mozilla.org/rdf/datasource;1?name=in-memory-datasource'].createInstance(Components.interfaces.nsIRDFDataSource);};
  70. GoogleRedesignedRDFDataSource.prototype.getAllResources = function () {if (this.datasource == null) {return null;}return new GoogleRedesignedRDFEnumerator(this.datasource.GetAllResources(), this.datasource);};
  71. GoogleRedesignedRDFDataSource.prototype.getRawDataSource = function () {if (this.datasource == null) {this.makeemptyds();}return this.datasource;};
  72. GoogleRedesignedRDFDataSource.prototype.getNode = function (uri) {if (this.datasource == null) {this.makeemptyds();}var node = new GoogleRedesignedRDFNode(uri, this);return node;};
  73. GoogleRedesignedRDFDataSource.prototype.getAnonymousNode = function () {if (this.datasource == null) {this.makeemptyds();}var anon = RDFService.GetAnonymousResource();var node = new GoogleRedesignedRDFNode;node.Init(anon, this.datasource);return node;};
  74. GoogleRedesignedRDFDataSource.prototype.getLiteral = function (uri) {if (this.datasource == null) {this.makeemptyds();}return new GoogleRedesignedRDFLiteral(uri, this);};
  75. GoogleRedesignedRDFDataSource.prototype.refresh = function (sync) {try {var ds = this.datasource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);ds.Refresh(sync);return true;} catch (ex) {return false;}};
  76. GoogleRedesignedRDFDataSource.prototype.save = function () {try {var ds = this.datasource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);ds.Flush();return true;} catch (ex) {return false;}};
  77. GoogleRedesignedRDFDataSource.prototype.copyAllToDataSource = function (dsource2) {if (this.datasource == null) {this.makeemptyds();}if (dsource2.datasource == null) {dsource2.makeemptyds();}var dsource1 = this.datasource;dsource2 = dsource2.datasource;var sourcelist = dsource1.GetAllResources();while (sourcelist.hasMoreElements()) {var source = sourcelist.getNext();var props = dsource1.ArcLabelsOut(source);while (props.hasMoreElements()) {var prop = props.getNext();prop = prop.QueryInterface(Components.interfaces.nsIRDFResource);var target = dsource1.GetTarget(source, prop, true);if (target != null) {dsource2.Assert(source, prop, target, true);}}}};
  78. GoogleRedesignedRDFDataSource.prototype.deleteRecursive = function (val) {var node;var dsource = this.datasource;if (dsource == null) {return;}if (typeof val == "string") {node = RDFService.GetResource(val);} else {node = val.source;}this.deleteRecursiveH(dsource, node);var props = dsource.ArcLabelsIn(node);while (props.hasMoreElements()) {var prop = props.getNext();var source = dsource.GetSource(prop, node, true);dsource.Unassert(source, prop, node);}};
  79. GoogleRedesignedRDFDataSource.prototype.deleteRecursiveH = function (dsource, node) {var props = dsource.ArcLabelsOut(node);while (props.hasMoreElements()) {var prop = props.getNext();var targets = dsource.GetTargets(node, prop, true);while (targets.hasMoreElements()) {var target = targets.getNext();try {target = target.QueryInterface(Components.interfaces.nsIRDFResource);this.deleteRecursiveH(dsource, target);} catch (e) {}dsource.Unassert(node, prop, target);}}};
  80. function GoogleRedesignedRDFNode(uri, dsource) {
  81.     if (uri == null) {
  82.         this.source = null;
  83.     } else {
  84.         this.source = RDFService.GetResource(uri);
  85.     }
  86.     if (dsource == null) {
  87.         this.datasource = null;
  88.     } else {
  89.         this.datasource = dsource.datasource;
  90.     }
  91.     this.container = null;
  92. }
  93. GoogleRedesignedRDFNode.prototype.Init = function (source, dsource) {this.source = source;this.datasource = dsource;this.container = null;};
  94. GoogleRedesignedRDFNode.prototype.getValue = function () {return this.source.Value;};
  95. GoogleRedesignedRDFNode.prototype.rlify = function (val) {var res = null;if (val != null) {try {val = val.QueryInterface(Components.interfaces.nsIRDFResource);res = new GoogleRedesignedRDFNode;res.Init(val, this.datasource);} catch (ex) {try {val = val.QueryInterface(Components.interfaces.nsIRDFLiteral);res = new GoogleRedesignedRDFLiteral;res.Init(val, this.datasource);} catch (ex2) {}}}return res;};
  96. GoogleRedesignedRDFNode.prototype.makeres = function (val) {if (typeof val == "string") {return RDFService.GetResource(val);} else {return val.source;}};
  97. GoogleRedesignedRDFNode.prototype.makelit = function (val) {if (typeof val == "string") {return RDFService.GetLiteral(val);} else {return val.source;}};
  98. GoogleRedesignedRDFNode.prototype.makecontain = function () {if (this.container != null) {return true;}var RDFContainer = "@mozilla.org/rdf/container;1";RDFContainer = Components.classes[RDFContainer].createInstance();RDFContainer = RDFContainer.QueryInterface(Components.interfaces.nsIRDFContainer);try {RDFContainer.Init(this.datasource, this.source);this.container = RDFContainer;return true;} catch (ex) {return false;}};
  99. GoogleRedesignedRDFNode.prototype.addTarget = function (prop, target) {prop = this.makeres(prop);target = this.makelit(target);this.datasource.Assert(this.source, prop, target, true);};
  100. GoogleRedesignedRDFNode.prototype.addTargetOnce = function (prop, target) {prop = this.makeres(prop);target = this.makelit(target);var oldtarget = this.datasource.GetTarget(this.source, prop, true);if (oldtarget != null) {this.datasource.Change(this.source, prop, oldtarget, target);} else {this.datasource.Assert(this.source, prop, target, true);}};
  101. GoogleRedesignedRDFNode.prototype.modifyTarget = function (prop, oldtarget, newtarget) {prop = this.makeres(prop);oldtarget = this.makelit(oldtarget);newtarget = this.makelit(newtarget);this.datasource.Change(this.source, prop, oldtarget, newtarget);};
  102. GoogleRedesignedRDFNode.prototype.modifySource = function (prop, oldsource, newsource) {prop = this.makeres(prop);oldsource = this.makeres(oldsource);newsource = this.makeres(newsource);this.datasource.Move(oldsource, newsource, prop, this.source);};
  103. GoogleRedesignedRDFNode.prototype.targetExists = function (prop, target) {prop = this.makeres(prop);target = this.makelit(target);return this.datasource.HasAssertion(this.source, prop, target, true);};
  104. GoogleRedesignedRDFNode.prototype.removeTarget = function (prop, target) {prop = this.makeres(prop);target = this.makelit(target);this.datasource.Unassert(this.source, prop, target);};
  105. GoogleRedesignedRDFNode.prototype.getProperties = function () {return new GoogleRedesignedRDFEnumerator(this.datasource.ArcLabelsOut(this.source), this.datasource);};
  106. GoogleRedesignedRDFNode.prototype.getInProperties = function () {return new GoogleRedesignedRDFEnumerator(this.datasource.ArcLabelsIn(this.source), this.datasource);};
  107. GoogleRedesignedRDFNode.prototype.propertyExists = function (prop) {prop = this.makeres(prop);return this.datasource.hasArcOut(this.source, prop);};
  108. GoogleRedesignedRDFNode.prototype.inPropertyExists = function (prop) {prop = this.makeres(prop);return this.datasource.hasArcIn(this.source, prop);};
  109. GoogleRedesignedRDFNode.prototype.getTarget = function (prop) {prop = this.makeres(prop);return this.rlify(this.datasource.GetTarget(this.source, prop, true));};
  110. GoogleRedesignedRDFNode.prototype.getSource = function (prop) {prop = this.makeres(prop);var src = this.datasource.GetSource(prop, this.source, true);if (src == null) {return null;}var res = new GoogleRedesignedRDFNode;res.Init(src, this.datasource);return res;};
  111. GoogleRedesignedRDFNode.prototype.getTargets = function (prop) {prop = this.makeres(prop);return new GoogleRedesignedRDFEnumerator(this.datasource.GetTargets(this.source, prop, true), this.datasource);};
  112. GoogleRedesignedRDFNode.prototype.getSources = function (prop) {prop = this.makeres(prop);return new GoogleRedesignedRDFEnumerator(this.datasource.GetSources(prop, this.source, true), this.datasource);};
  113. GoogleRedesignedRDFNode.prototype.makeBag = function () {this.container = RDFContainerUtilsService.MakeBag(this.datasource, this.source);};
  114. GoogleRedesignedRDFNode.prototype.makeSeq = function () {this.container = RDFContainerUtilsService.MakeSeq(this.datasource, this.source);};
  115. GoogleRedesignedRDFNode.prototype.makeAlt = function () {this.container = RDFContainerUtilsService.MakeAlt(this.datasource, this.source);};
  116. GoogleRedesignedRDFNode.prototype.isBag = function () {return RDFContainerUtilsService.IsBag(this.datasource, this.source);};
  117. GoogleRedesignedRDFNode.prototype.isSeq = function () {return RDFContainerUtilsService.IsSeq(this.datasource, this.source);};
  118. GoogleRedesignedRDFNode.prototype.isAlt = function () {return RDFContainerUtilsService.IsAlt(this.datasource, this.source);};
  119. GoogleRedesignedRDFNode.prototype.isContainer = function () {return RDFContainerUtilsService.IsContainer(this.datasource, this.source);};
  120. GoogleRedesignedRDFNode.prototype.getChildCount = function () {if (this.makecontain()) {return this.container.GetCount();}return -1;};
  121. GoogleRedesignedRDFNode.prototype.getChildren = function () {if (this.makecontain()) {return new GoogleRedesignedRDFEnumerator(this.container.GetElements(), this.datasource);} else {return null;}};
  122. GoogleRedesignedRDFNode.prototype.addChild = function (child, exists) {if (this.makecontain()) {var childres = null;if (typeof child == "string") {childres = RDFService.GetResource(child);child = new GoogleRedesignedRDFNode;child.Init(childres, this.datasource);} else {childres = child.source;}if (!exists && this.container.IndexOf(childres) >= 0) {return child;}this.container.AppendElement(childres);return child;} else {return null;}};
  123. GoogleRedesignedRDFNode.prototype.addChildAt = function (child, idx) {if (this.makecontain()) {var childres = null;if (typeof child == "string") {childres = RDFService.GetResource(child);child = new GoogleRedesignedRDFNode;child.Init(childres, this.datasource);} else {childres = child.source;}this.container.InsertElementAt(childres, idx, true);return child;} else {return null;}};
  124. GoogleRedesignedRDFNode.prototype.removeChild = function (child) {if (this.makecontain()) {var childres = null;if (typeof child == "string") {childres = RDFService.GetResource(child);child = new GoogleRedesignedRDFNode;child.Init(childres, this.datasource);} else {childres = child.source;}this.container.RemoveElement(childres, true);return child;} else {return null;}};
  125. GoogleRedesignedRDFNode.prototype.removeChildAt = function (idx) {if (this.makecontain()) {var childres = this.container.RemoveElementAt(idx, true);return this.rlify(childres);} else {return null;}};
  126. GoogleRedesignedRDFNode.prototype.getChildIndex = function (child) {if (this.makecontain()) {return this.container.IndexOf(child.source);} else {return -1;}};
  127. GoogleRedesignedRDFNode.prototype.type = "Node";
  128. function GoogleRedesignedRDFLiteral(val, dsource) {
  129.     if (val == null) {
  130.         this.source = null;
  131.     } else {
  132.         this.source = RDFService.GetLiteral(val);
  133.     }
  134.     if (dsource == null) {
  135.         this.datasource = null;
  136.     } else {
  137.         this.datasource = dsource.datasource;
  138.     }
  139. }
  140. GoogleRedesignedRDFLiteral.prototype.Init = function (source, dsource) {this.source = source;this.datasource = dsource;};
  141. GoogleRedesignedRDFLiteral.prototype.getValue = function () {return this.source.Value;};
  142. GoogleRedesignedRDFLiteral.prototype.makeres = function (val) {if (typeof val == "string") {return RDFService.GetResource(val);} else {return val.source;}};
  143. GoogleRedesignedRDFLiteral.prototype.makelit = function (val) {if (typeof val == "string") {return RDFService.GetLiteral(val);} else {return val.source;}};
  144. GoogleRedesignedRDFLiteral.prototype.modifySource = function (prop, oldsource, newsource) {prop = this.makeres(prop);oldsource = this.makeres(oldsource);newsource = this.makeres(newsource);this.datasource.Move(oldsource, newsource, prop, this.source);};
  145. GoogleRedesignedRDFLiteral.prototype.getInProperties = function (prop) {return new GoogleRedesignedRDFEnumerator(this.datasource.ArcLabelsIn(this.source), this.datasource);};
  146. GoogleRedesignedRDFLiteral.prototype.inPropertyExists = function (prop) {prop = this.makeres(prop);return this.datasource.hasArcIn(this.source, prop);};
  147. GoogleRedesignedRDFLiteral.prototype.getSource = function (prop) {prop = this.makeres(prop);var src = this.datasource.GetSource(prop, this.source, true);if (src == null) {return null;}var res = new GoogleRedesignedRDFNode;res.Init(src, this.datasource);return res;};
  148. GoogleRedesignedRDFLiteral.prototype.getSources = function (prop) {prop = this.makeres(prop);return new GoogleRedesignedRDFEnumerator(this.datasource.GetSources(prop, this.source, true), this.datasource);};
  149. GoogleRedesignedRDFLiteral.prototype.type = "Literal";
  150. function GoogleRedesignedRDFEnumerator(enumeration, dsource) {
  151.     this.enumeration = enumeration;
  152.     this.datasource = dsource;
  153. }
  154. GoogleRedesignedRDFEnumerator.prototype.hasMoreElements = function () {return this.enumeration.hasMoreElements();};
  155. GoogleRedesignedRDFEnumerator.prototype.getNext = function () {var res = null;var val = this.enumeration.getNext();if (val != null) {try {val = val.QueryInterface(Components.interfaces.nsIRDFResource);res = new GoogleRedesignedRDFNode;res.Init(val, this.datasource);} catch (ex) {try {val = val.QueryInterface(Components.interfaces.nsIRDFLiteral);res = new GoogleRedesignedRDFLiteral;res.Init(val, this.datasource);} catch (ex2) {}}}return res;};
  156.